package edu.pku.hawk.service.impl;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;

import edu.pku.hawk.controller.util.Config;
import edu.pku.hawk.controller.util.Constants;
import edu.pku.hawk.controller.util.SensorAllData;
import edu.pku.hawk.controller.util.SensorAllData2;
import edu.pku.hawk.controller.util.Util;
import edu.pku.hawk.controller.util.Constants.ServiceAccessMode;
import edu.pku.hawk.datasource.ConnectDB;
import edu.pku.hawk.datasource.SensorType;
import edu.pku.hawk.service.SensorDataSer;
import edu.pku.hawk.service.bo.RowSensorData;

public class SensorDataSerImpl_Local implements SensorDataSer {

	private ConnectDB DBConnection;

	public SensorDataSerImpl_Local() {
		if (Config.DBSetting == ServiceAccessMode.LocalDBService) {
			try {
				if (Constants.DataSource_Using) {
					System.out.print("Connecting to " + Constants.DataSource_URL + " ...");
					DBConnection = ConnectDB.getDBConnection(Constants.DataSource_Name, Constants.DataSource_Password,
							Constants.DataSource_URL, Constants.DataSource_DB);
					System.out.println("Down.");
				} else {
					System.out.println("----------------Do not use DataSource.");
				}

				// if (Constants.RFID_Using) {
				// System.out.print("Connecting to " + Constants.RFID_URL + "
				// ...");
				// rfidCon = ConnectDB.getDBConnection(Constants.RFID_Name,
				// Constants.RFID_Password, Constants.RFID_URL,
				// Constants.RFID_DB);
				// System.out.println("Down.");
				// } else {
				// System.out.println("-----------------Do not use RFID.");
				// }

				// sensorDataByDevice.put(1, new SensorAllData());
				// sensorDataByDevice.put(2, new SensorAllData());
				// sensorDataByDevice.put(3, new SensorAllData());
				// sensorDataByDevice.put(4, new SensorAllData());

				System.out.println("Down.");
			} catch (Exception ex) {
				ex.printStackTrace();
			}

		} else if (Config.DBSetting == ServiceAccessMode.DistributedCloud) {

		}
	}

	public HashMap<Integer, SensorAllData> queryAll() {
		HashMap<Integer, SensorAllData> ret = new HashMap<Integer, SensorAllData>();
		try {
			ConnectDB con = ConnectDB.getDBConnection(Constants.DataSource_Name, Constants.DataSource_Password,
					Constants.DataSource_URL, Constants.DataSource_DB);
			String s = new String();
			s = "select * from sensors";
			Statement stmt = con.createStatement();
			ResultSet result = stmt.executeQuery(s);

			while (result.next()) {
				SensorAllData sad = new SensorAllData();
				sad.device_address = result.getInt("device_address");
				sad.io_value = result.getInt("io_value");
				sad.pressure_1 = result.getInt("pressure_1");
				sad.pressure_2 = result.getInt("pressure_2");
				sad.pressure_3 = result.getInt("pressure_3");
				sad.pressure_4 = result.getInt("pressure_4");
				sad.time = result.getTimestamp("time");
				ret.put(sad.device_address, sad);

//				Util.logln(" =============" + sad.device_address + " " + sad.io_value + " " + sad.pressure_1 + " "
//						+ sad.pressure_2 + " " + sad.pressure_3 + " " + sad.pressure_4);
			}

			stmt.close();
			stmt = null;
			result.close();
			result = null;

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return ret;
	}

	public RowSensorData getBySensorCompact(String sensorType, int sensorId) {

		SensorType sensor = SensorType.valueOf(sensorType);
		if (sensor == SensorType.sensor_rfid) {
			RowSensorData tm = RowSensorData.Empty;
			try {
				String s = new String();
				s = "select * from " + sensor + " where sensor_id=" + sensorId;
				// s = "select * from " + sensor;
				ConnectDB con = ConnectDB.getDBConnection(Constants.DataSource_Name, Constants.DataSource_Password,
						Constants.DataSource_URL, Constants.DataSource_DB);
				Statement stmt = con.createStatement();
				ResultSet result = stmt.executeQuery(s);

				while (result.next()) {
					Object value = result.getString("value");
					java.sql.Timestamp time = result.getTimestamp("time");
					int sid = result.getInt("sensor_id");
					tm = new RowSensorData(sid, value, time);
				}
				result.close();
				result = null;
				stmt.close();
				stmt = null;
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return tm;

		} else if (sensor == SensorType.sensor_sound) {
			RowSensorData tm = RowSensorData.Empty;
			try {
				String s = new String();
				s = "select * from " + sensor;
				ConnectDB con = ConnectDB.getDBConnection(Constants.DataSource_Name, Constants.DataSource_Password,
						Constants.DataSource_URL, Constants.DataSource_DB);
				Statement stmt = con.createStatement();
				ResultSet result = stmt.executeQuery(s);

				while (result.next()) {
					Object value = result.getString("value");
					java.sql.Timestamp time = result.getTimestamp("time");
					int sid = result.getInt("sensor_id");
					tm = new RowSensorData(sid, value, time);
				}
				result.close();
				result = null;
				stmt.close();
				stmt = null;
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return tm;

		} else {
			return RowSensorData.Empty;
		}
	}
	//
	// // unused
	// public RowSensorData getLastValueBySensor(String sensorType, int
	// sensorId) {
	// if (Config.DBSetting == ServiceAccessMode.FakeDBService) {
	// return new RowSensorData(2, 1, new Timestamp(1234565432));
	//
	// } else if (Config.DBSetting == ServiceAccessMode.LocalDBService) {
	// try {
	// SensorData data = GetValue.getLastValueBySensor(DBConnection,
	// SensorType.valueOf(sensorType), sensorId);
	//
	// RowSensorData rd;
	// if (data != null)
	// rd = new RowSensorData(data.getsensorId(), data.getvalue(),
	// data.gettime());
	// else
	// rd = RowSensorData.Empty;
	// return rd;
	//
	// // return RowSensorData.Default;
	//
	// } catch (Exception ex) {
	// ex.printStackTrace();
	// return null;
	// }
	//
	// } else if (Config.DBSetting == ServiceAccessMode.DistributedCloud) {
	// if (sensorType.equalsIgnoreCase(Constants.Infrared_TableName)) {
	//
	// } else if (sensorType.equalsIgnoreCase(Constants.Pressure_TableName)) {
	//
	// } else if (sensorType.equalsIgnoreCase(Constants.Sound_TableName)) {
	//
	// } else if (sensorType.equalsIgnoreCase(Constants.Metal_TableName)) {
	//
	// } else {
	// return null;
	// }
	// return null;
	//
	// } else {
	// return null;
	// }
	// }
	//
	// // unused
	// public RowSensorData[] getValueBySensorAndTimeSegment(String sensorType,
	// int sensorId, Timestamp start,
	// Timestamp end) {
	// if (Config.DBSetting == ServiceAccessMode.FakeDBService) {
	// return new RowSensorData[] { new RowSensorData(2, 1, new
	// Timestamp(1234565432)) };
	//
	// } else if (Config.DBSetting == ServiceAccessMode.LocalDBService) {
	// try {
	// SensorData[] data = GetValue.getValueBySensorAndTimeSegment(DBConnection,
	// SensorType
	// .valueOf(sensorType), sensorId, start, end);
	// if (data != null && data.length > 0) {
	// RowSensorData[] rds = new RowSensorData[data.length];
	// for (int i = 0; i < rds.length; i++) {
	// rds[i] = new RowSensorData(data[i].getsensorId(), data[i].getvalue(),
	// data[i].gettime());
	// }
	// return rds;
	// } else {
	// return null;
	// }
	// } catch (Exception ex) {
	// ex.printStackTrace();
	// return null;
	// }
	//
	// } else if (Config.DBSetting == ServiceAccessMode.DistributedCloud) {
	// if (sensorType.equalsIgnoreCase(Constants.Infrared_TableName)) {
	//
	// } else if (sensorType.equalsIgnoreCase(Constants.Pressure_TableName)) {
	//
	// } else if (sensorType.equalsIgnoreCase(Constants.Sound_TableName)) {
	//
	// } else if (sensorType.equalsIgnoreCase(Constants.Metal_TableName)) {
	//
	// } else {
	// return null;
	// }
	// return null;
	//
	// } else {
	// return null;
	// }
	//
	// }

	@Override
	public HashMap<Integer, SensorAllData2> queryAll2() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SensorAllData[] queryAllWithArray() {
		// TODO Auto-generated method stub
		return null;
	}

}
